package com.hankol.feature_wine_meal.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.baselibrary.view.BanViewPager;
import com.baselibrary.view.PagerIndicatorView;
import com.hankol.feature_wine_meal.CuisineActivity;
import com.hankol.feature_wine_meal.R;
import com.hankol.feature_wine_meal.adapter.EliteTabAdapter;
import com.hankol.feature_wine_meal.adapter.EliteVpFragmentAdapter;
import com.hankol.feature_wine_meal.adapter.HeadBannerVpAdapter;
import com.hankol.feature_wine_meal.bean.CharacteristicBean;
import com.hankol.feature_wine_meal.bean.RecommendFoodBean;
import com.hankol.feature_wine_meal.viewmodel.WineFoodViewModel;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author: 关忆北、
 * @date: 2022/6/20
 * @Des：下酒菜头部布局
 */
public class WineFoodHeadView extends LinearLayout {
    private LinearLayout mLlWineFoodBanner, mLlWineFoodElite;
    private ViewPager mVpWineFood;
    private BanViewPager mVpWineFoodElite;
    private PagerIndicatorView mPivWineFood;
    private LinearLayout mLlWineFoodCuisine;
    private RecyclerView mRvbWineFoodElite;

    private final EliteTabAdapter mEliteTabAdapter = new EliteTabAdapter();//精选推荐Tab
    private final HeadBannerVpAdapter mHeadBannerVpAdapter = new HeadBannerVpAdapter();
    private ArrayList<CharacteristicBean> characteristicList = new ArrayList<>();
    private WineFoodViewModel.OnBannerLooperListener mBannerLooperListener;
    private EliteVpFragmentAdapter mEliteVpAdapter;
    private int currentPage = 1;

    public WineFoodHeadView(Context context) {
        super(context);
        initView();
    }

    public WineFoodHeadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WineFoodHeadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.view_wine_food_head, this);
        mLlWineFoodBanner = (LinearLayout) findViewById(R.id.ll_wineFood_banner);
        mVpWineFood = (ViewPager) findViewById(R.id.vp_wineFood);
        mPivWineFood = (PagerIndicatorView) findViewById(R.id.piv_wineFood);
        mLlWineFoodCuisine = (LinearLayout) findViewById(R.id.ll_wineFood_cuisine);
        mLlWineFoodElite = (LinearLayout) findViewById(R.id.ll_wineFood_elite);
        mRvbWineFoodElite = (RecyclerView) findViewById(R.id.rv_wineFood_elite);
        mVpWineFoodElite = (BanViewPager) findViewById(R.id.vp_wineFood_elite);
        initListener();

        mVpWineFood.setAdapter(mHeadBannerVpAdapter);
        mRvbWineFoodElite.setAdapter(mEliteTabAdapter);


    }

    public void setFragmentManager(FragmentManager fm) {
        mEliteVpAdapter = new EliteVpFragmentAdapter(fm);
        mVpWineFoodElite.setAdapter(mEliteVpAdapter);
    }

    /**
     * 推荐列表
     *
     * @param beans
     */
    public void setRecommend(ArrayList<RecommendFoodBean> beans) {
        if (beans != null) {
            Iterator<RecommendFoodBean> iterator = beans.iterator();
            while (iterator.hasNext()) {
                RecommendFoodBean bean = iterator.next();
                if (bean.getAppetizerDietList().size() <= 0) {
                    iterator.remove();
                }

            }

            if (beans.size() > 0) {
                mLlWineFoodElite.setVisibility(VISIBLE);
                //精选推荐的tab
                beans.get(0).setSelected(true);
                mEliteTabAdapter.setNewData(beans);

                //精选推荐viewPager
                mEliteVpAdapter.setData(beans);
                mVpWineFoodElite.setCurrentItem(0, true);
            } else {
                //隐藏精选推荐
                mLlWineFoodElite.setVisibility(GONE);
            }
        } else {
            //隐藏精选推荐
            mLlWineFoodElite.setVisibility(GONE);
        }

    }


    /**
     * 特色菜系
     *
     * @param list
     */
    public void setCharacteristic(ArrayList<CharacteristicBean> list) {
        if (list != null) {
            characteristicList.clear();
            mPivWineFood.setSumCount(list.size());

            list.add(list.get(0));
            list.add(0, list.get(list.size() - 1));
            mHeadBannerVpAdapter.setData(list);
            characteristicList.addAll(list);
            mVpWineFood.setOffscreenPageLimit(list.size());
            mVpWineFood.setCurrentItem(currentPage, true);
            mPivWineFood.setCurrentShowView((currentPage - 1 + mPivWineFood.getCount()) % mPivWineFood.getCount());
        }
    }

    private void initListener() {
        mBannerLooperListener = () -> {
            //通知进行轮播
            if (mHeadBannerVpAdapter.getHashMap().size() > 0) {
                // mHeadBannerVpAdapter.getHashMap().get(mSelectedPosition).startTranslateAnimation();
            }
        };
        mVpWineFood.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
                mPivWineFood.setCurrentShowView((position - 1 + mPivWineFood.getCount()) % mPivWineFood.getCount());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

                //验证当前的滑动是否结束
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    //滑动动画做完的时候
                    if (currentPage == 0) {
                        mVpWineFood.setCurrentItem(characteristicList.size() - 2, false); //切换，不要动画效果
                    } else if (currentPage == characteristicList.size() - 1) {
                        mVpWineFood.setCurrentItem(1, false);//切换，不要动画效果
                    }
                }
            }
        });

        mLlWineFoodCuisine.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), CuisineActivity.class);
            getContext().startActivity(intent);
        });
        mEliteTabAdapter.setOnItemClickListener((adapter, view, position) -> {
            mEliteTabAdapter.upSelected(position);
            mVpWineFoodElite.setCurrentItem(position, true);
        });
    }
}
